<?php

App::uses('AppController', 'Controller');

/**
 * Chapters Controller
 *
 * @property Chapter $Chapter
 */
class ChaptersController extends AppController {
    public $helpers = array('Form','GdgUpload.Upload');
    public $scaffold = 'admin';
    public $cacheAction = array(
        'view' => "1 hour",
        'index'  => "1 hour"
    );
    
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->deny(array('admin_index', 'admin_view','admin_add','admin_edit', 'admin_delete'));
    }
    
    public function index() {
        $this->Chapter->recursive = 0;
        $this->set('chapters', $this->Chapter->find('all',array('order'=>'name')));
    }
    
    public function view($slug=null){
        
        $this->Chapter->bindModel(array(
            'hasMany' => array(
                'Event'=> array(
                    'fields' => array('Event.id', 'Event.name', 'Event.startdate', 'Event.accepting', 'Event.slug','Event.place_id'),
                    'conditions'    => array('active'=>1, 'Event.startdate >=' => date('Y-m-d') ),
                    'order' => 'Event.startdate ASC'
                ))
        ));
        $this->Chapter->recursive = 0;
        $chapter = $this->Chapter->find('first', array(
            'conditions' => array('Chapter.slug' => $slug),
            'contain'   => array(
                'Event' => array(
                    'Place' => array('Address')
                )
            )
        ));
        
        if (!$chapter) {
            throw new NotFoundException(__('Invalid chapter'));
        }
        $this->helpers[] = 'Timepl';
        $this->set('chapter', $chapter);
    }
    
    /**
     * admin_index method
     *
     * @return void
     */
//    public function admin_index() {
//        $this->Chapter->recursive = 0;
//        $this->set('chapters', $this->paginate());
//    }

    /**
     * admin_view method
     *
     * @param string $id
     * @return void
     */
    public function admin_view($id = null) {
        $this->Chapter->id = $id;
        if (!$this->Chapter->exists()) {
            throw new NotFoundException(__('Invalid chapter'));
        }
        $this->Chapter->recursive = 0;
        $this->set('chapter', $this->Chapter->read(null, $id));
    }

    /**
     * admin_add method
     *
     * @return void
     */
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->Chapter->create();
            if ($this->Chapter->save($this->request->data)) {
                $this->Session->setFlash(__('The chapter has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The chapter could not be saved. Please, try again.'));
            }
        }
    }

    /**
     * admin_edit method
     *
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        $this->Chapter->id = $id;
        if (!$this->Chapter->exists()) {
            throw new NotFoundException(__('Invalid chapter'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Chapter->save($this->request->data)) {
                $this->Session->setFlash(__('The chapter has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The chapter could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Chapter->read(null, $id);
        }
    }

    /**
     * admin_delete method
     *
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Chapter->id = $id;
        if (!$this->Chapter->exists()) {
            throw new NotFoundException(__('Invalid chapter'));
        }
        if ($this->Chapter->delete()) {
            $this->Session->setFlash(__('Chapter deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Chapter was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

}
